package com.ruoyi.yard.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yard.domain.*;
import com.ruoyi.yard.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yard.service.IDeliveryPlanInfoService;

/**
 * 发货计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-05
 */
@Service
public class DeliveryPlanInfoServiceImpl implements IDeliveryPlanInfoService 
{
    @Autowired
    private DeliveryPlanInfoMapper deliveryPlanInfoMapper;

    @Autowired
    private DeliveryCompRefMapper deliveryCompRefMapper;

    @Autowired
    WaybillInfoMapper waybillInfoMapper;

    @Autowired
    WaybillCompRefMapper waybillCompRefMapper;

    @Autowired
    CompWarehouseInfoMapper compWarehouseInfoMapper;

    /**
     * 查询发货计划
     * 
     * @param id 发货计划主键
     * @return 发货计划
     */
    @Override
    public DeliveryPlanInfo selectDeliveryPlanInfoById(Long id)
    {
        DeliveryPlanInfo deliveryPlanInfo = deliveryPlanInfoMapper.selectDeliveryPlanInfoById(id);

        List<CompWarehouseInfo> compWarehouseInfoList=compWarehouseInfoMapper.selectCompWarehouseInfoByPlayId(deliveryPlanInfo.getDeliveryPlanNo());
        String deliveryPlanNo = deliveryPlanInfo.getDeliveryPlanNo();

        for (CompWarehouseInfo c : compWarehouseInfoList) {
            //根据发货计划号和零件编号 查发运单再查数量 并是已审核过的
            c.setActualCount(compWarehouseInfoMapper.selectCount(deliveryPlanNo,c.getWarehouseCompomentNo()));
        }
        deliveryPlanInfo.setCompWarehouseInfoList(compWarehouseInfoList);
        return deliveryPlanInfo;
    }

    /**
     * 查询发货计划列表
     * 
     * @param deliveryPlanInfo 发货计划
     * @return 发货计划
     */
    @Override
    public List<DeliveryPlanInfo> selectDeliveryPlanInfoList(DeliveryPlanInfo deliveryPlanInfo)
    {
        return deliveryPlanInfoMapper.selectDeliveryPlanInfoList(deliveryPlanInfo);
    }

    @Override
    public List<DeliveryPlanInfo> homePageList(DeliveryPlanInfo deliveryPlanInfo) {
        List<DeliveryPlanInfo> deliveryPlanInfos = deliveryPlanInfoMapper.homePageList(deliveryPlanInfo);
        for (DeliveryPlanInfo d:deliveryPlanInfos) {
            //查询当前发货计划的 发货计划构件表
            List<DeliveryCompRef> deliveryCompRefList = deliveryPlanInfoMapper.selectDeliveryPlanInfoByPlayNo(d.getDeliveryPlanNo());
            Long count=0L;
            Double weight=0D;
            for (DeliveryCompRef deliveryCompRef : deliveryCompRefList) {
                count=count+deliveryCompRef.getNeedCount();
                weight=weight+deliveryCompRef.getWeight();
            }
            d.setDeliveryCompRefList(deliveryCompRefList);
            d.setPlanCount(count);
            d.setPlanWeight(weight/1000);

            count=0L;
            weight=0D;
            List<WaybillCompRef> waybillCompRefList=waybillCompRefMapper.selectWaybillInfoByPlayId(d.getDeliveryPlanNo());
            for (WaybillCompRef waybillCompRef:waybillCompRefList){
                count=count+waybillCompRef.getCount();
                weight=weight+waybillCompRef.getWeight();
            }
            d.setActualCount(count);
            d.setActualWeight(weight/1000);
            d.setWaybillInfoList(waybillInfoMapper.selectWaybillInfoByPlayId(d.getDeliveryPlanNo()));
        }
        return deliveryPlanInfos;
    }

    @Override
    public int toExamine(JSONObject params) {
        Long id = params.getLong("id");
            //0未送检、1已送检、2通过、3被驳回、4已完成
        Integer state = params.getInteger("state");
        DeliveryPlanInfo deliveryPlanInfo1 = deliveryPlanInfoMapper.selectDeliveryPlanInfoById(id);
        if (deliveryPlanInfo1==null)
            throw new ServiceException("发货计划不存在！") ;
        if (StringUtils.isNull(id) || StringUtils.isNull(state))
            throw new ServiceException("参数错误！") ;
        DeliveryPlanInfo deliveryPlanInfo = deliveryPlanInfoMapper.selectDeliveryPlanInfoById(id);
        if (deliveryPlanInfo.getDeliveryPlanState()==2 || deliveryPlanInfo.getDeliveryPlanState()==3 ||deliveryPlanInfo.getDeliveryPlanState()==4){
            throw new ServiceException("发货计划已审核") ;
        }
        if (state==2){//审核通过,扣库存
            DeliveryCompRef deliveryCompRef=new DeliveryCompRef();
            deliveryCompRef.setDeliveryPlanNo(deliveryPlanInfo.getDeliveryPlanNo());
            //根据发货计划编号来查发货计划构建表

            List<DeliveryCompRef> deliveryCompRefList = deliveryCompRefMapper.selectDeliveryCompRefList(deliveryCompRef);
            for (DeliveryCompRef del:deliveryCompRefList){
                CompWarehouseInfo compWarehouseInfo=compWarehouseInfoMapper.selectCompWarehouseInfoByCompomentNo(del.getWarehouseCompomentNo());
                if (compWarehouseInfo==null){
                    throw new ServiceException("构建不存在") ;
                }
                if (compWarehouseInfo.getStockCount()<del.getNeedCount()){
                    throw new ServiceException("库存数量小于计划出库数量") ;
                }
                compWarehouseInfoMapper.updateReduceStock(del.getWarehouseCompomentNo(),compWarehouseInfo.getStockCount()-del.getNeedCount());
            }

        }
        DeliveryPlanInfo d=new DeliveryPlanInfo();
        d.setId(id);
        d.setDeliveryPlanState(state);
        return deliveryPlanInfoMapper.updateDeliveryPlanInfo(d);
    }

    /**
     * 新增发货计划
     * 
     * @param deliveryPlanInfo 发货计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDeliveryPlanInfo(DeliveryPlanInfo deliveryPlanInfo)
    {
        deliveryPlanInfo.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        deliveryPlanInfo.setCreateBy(user.getUserName());
        deliveryPlanInfo.setDeliveryPlanNo(GenCodeUtils.getDeliveryPlanNo());
        int rows = deliveryPlanInfoMapper.insertDeliveryPlanInfo(deliveryPlanInfo);

        insertDeliveryCompRef(deliveryPlanInfo);
        return rows;
    }

    /**
     * 修改发货计划
     * 
     * @param deliveryPlanInfo 发货计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDeliveryPlanInfo(DeliveryPlanInfo deliveryPlanInfo)
    {
        if (deliveryPlanInfo.getId()==null)
            throw new ServiceException("发货计划id不能为空");
        DeliveryPlanInfo deliveryPlanInfo1 = deliveryPlanInfoMapper.selectDeliveryPlanInfoById(deliveryPlanInfo.getId());
        if (deliveryPlanInfo1==null){
            throw new ServiceException("发货计划不存在");
        }
        if (deliveryPlanInfo1.getDeliveryPlanState()==2){
            throw new ServiceException("发货计划已审核,无法修改");
        }
        if (deliveryPlanInfo1.getDeliveryPlanState()==3){
            throw new ServiceException("发货计划已审核,无法修改");
        }
        if (deliveryPlanInfo1.getDeliveryPlanState()==4){
            throw new ServiceException("发货计划已完成,无法修改");
        }
        deliveryPlanInfo.setId(deliveryPlanInfo1.getId());
        deliveryPlanInfo.setDeliveryPlanNo(deliveryPlanInfo1.getDeliveryPlanNo());
        deliveryPlanInfo.setUpdateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        deliveryPlanInfo.setUpdateBy(user.getUserName());
        deliveryPlanInfoMapper.deleteDeliveryCompRefByWarehouseCompomentNo(deliveryPlanInfo.getDeliveryPlanNo());
        insertDeliveryCompRef(deliveryPlanInfo);
        return deliveryPlanInfoMapper.updateDeliveryPlanInfo(deliveryPlanInfo);
    }

    /**
     * 批量删除发货计划
     * 
     * @param ids 需要删除的发货计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeliveryPlanInfoByIds(Long[] ids)
    {

        deliveryPlanInfoMapper.deleteDeliveryCompRefByWarehouseCompomentNos(ids);
        return deliveryPlanInfoMapper.deleteDeliveryPlanInfoByIds(ids);
    }

    /**
     * 删除发货计划信息
     * 
     * @param id 发货计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeliveryPlanInfoById(Long id)
    {
        if (id==null)
            throw new ServiceException("发货计划id不能为空");
        DeliveryPlanInfo deliveryPlanInfo = deliveryPlanInfoMapper.selectDeliveryPlanInfoById(id);
        if (deliveryPlanInfo==null){
            throw new ServiceException("发货计划不存在");
        }
        if (deliveryPlanInfo.getDeliveryPlanState()==2){
            throw new ServiceException("发货计划已审核,无法删除");
        }
        if (deliveryPlanInfo.getDeliveryPlanState()==3){
            throw new ServiceException("发货计划已审核,无法删除");
        }
        if (deliveryPlanInfo.getDeliveryPlanState()==4){
            throw new ServiceException("发货计划已完成,无法删除");
        }
        deliveryPlanInfoMapper.deleteDeliveryCompRefByWarehouseCompomentNo(deliveryPlanInfo.getDeliveryPlanNo());
        return deliveryPlanInfoMapper.deleteDeliveryPlanInfoById(id);
    }



    /**
     * 新增发货计划_构件信息
     * 
     * @param deliveryPlanInfo 发货计划对象
     */
    public void insertDeliveryCompRef(DeliveryPlanInfo deliveryPlanInfo)
    {
        List<DeliveryCompRef> deliveryCompRefList = deliveryPlanInfo.getDeliveryCompRefList();
        String deliveryPlanNo = deliveryPlanInfo.getDeliveryPlanNo();
        if (StringUtils.isNotNull(deliveryCompRefList))
        {
            List<DeliveryCompRef> list = new ArrayList<DeliveryCompRef>();
            for (DeliveryCompRef deliveryCompRef : deliveryCompRefList)
            {
                deliveryCompRef.setDeliveryPlanNo(deliveryPlanNo);
                list.add(deliveryCompRef);
            }
            if (list.size() > 0)
            {// project_no, delivery_plan_no, warehouse_compoment_no, compoment_name,compoment_type, need_count
                deliveryPlanInfoMapper.batchDeliveryCompRef(list);
            }
        }
    }
}
